package com.ronghui.cash.util.lianlian.pay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lianpay.api.util.TraderRSAUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LianLianPayConfig {

	private static final String SIGN_TYPE = "RSA";
	private final static String NAME_GOODS = "分期贷款";
	private final static String PRI_KEY_VALUE = "+ID/G9QTTWqLfYwKTcToyKB608/dbmXw0=";
	public final static String OID_PARTNER = "201704271001686056";
	public final static String LIANLIAN_PUB_KEY_VALUE = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCSS/DiwdCf/aZsxxcacDnooGph3d2JOj5GXWi+q3gznZauZjkNP8SKl3J2liP0O6rU/Y/29+IUe+GTMhMOFJuZm1htAtKiu5ekW0GlBMWxf4FPkYlQkPE0FtaoMP3gYfh+OwI+fIRrpW3ySn3mScnc6Z700nU/VYrRkfcSCbSnRwIDAQAB";

	/**
	 *
	 * @return RSA
	 */
	public static String getSignType() {
		return SIGN_TYPE;
	}


	/**
	 *
	 * @return 分期贷款
	 */
	public static String getNameGoods() {
		return NAME_GOODS;
	}

	/**
	 * 生成签名
	 * @param obj
	 * @return
	 */
	public static String getSign(Object obj) {
		JSONObject reqObj = JSON.parseObject(JSON.toJSONString(obj));
		String sign = reqObj.getString("sign");
		System.out.println("===========sign=: " + sign);
		String sign_type = reqObj.getString("sign_type");
		System.out.println("===========sign_type=: " + sign_type);
		// // 生成待签名串
		String sign_src = genSignData(reqObj);
		System.out.println("商户[" + reqObj.getString("oid_partner") + "]待签名原串" + sign_src);
		System.out.println("商户[" + reqObj.getString("oid_partner") + "]签名串" + sign);

		if ("RSA".equals(sign_type)) {
			return getSignRSA(sign_src);
		}
		return null;
	}

	/**
	 * 验证签名
	 * @param reqObj
	 * @return
	 */
	public static boolean getNotifySign(JSONObject reqObj) {
		String sign = reqObj.getString("sign");
		System.out.println("===========sign=: " + sign);
		String sign_type = reqObj.getString("sign_type");
		System.out.println("===========sign_type=: " + sign_type);
		// // 生成待签名串
		String sign_src = genSignData(reqObj);
		System.out.println("商户[" + reqObj.getString("oid_partner") + "]待签名原串" + sign_src);
		System.out.println("商户[" + reqObj.getString("oid_partner") + "]签名串" + sign);

		if ("RSA".equals(sign_type)) {
			return getNotifySignRSA(sign_src,sign);
		}
		return false;
	}

	/**
	 * RSA签名验证
	 *
	 * @return
	 */
	public static String getSignRSA(String sign_src) {
		return TraderRSAUtil.sign(PRI_KEY_VALUE, sign_src);

	}

	/**
	 * RSA签名验证
	 *
	 * @param reqObj
	 * @return
	 */
	public static boolean getNotifySignRSA(String sign_src,String signed_str) {
		return TraderRSAUtil.checksign(LIANLIAN_PUB_KEY_VALUE, sign_src, signed_str);
	}

	/**
	 * 生成待签名串
	 *
	 * @param paramMap
	 * @return
	 */
	public static String genSignData(JSONObject jsonObject) {
		StringBuffer content = new StringBuffer();

		// 按照key做首字母升序排列
		List<String> keys = new ArrayList<String>(jsonObject.keySet());
		Collections.sort(keys, String.CASE_INSENSITIVE_ORDER);
		for (int i = 0; i < keys.size(); i++) {
			String key = keys.get(i);
			// sign 和ip_client 不参与签名
			if ("sign".equals(key)) {
				continue;
			}
			String value = jsonObject.getString(key);
			// 空串不参与签名
			if (null == value) {
				continue;
			}
			content.append((i == 0 ? "" : "&") + key + "=" + value);

		}
		String signSrc = content.toString();
		if (signSrc.startsWith("&")) {
			signSrc = signSrc.replaceFirst("&", "");
		}
		return signSrc;
	}

}
